package com.lhb.controller.admin;


import com.lhb.constant.JwtClaimsConstant;
import com.lhb.dto.UserAddDTO;
import com.lhb.dto.UserEditDTO;
import com.lhb.dto.UserLoginDTO;
import com.lhb.dto.UserPageQueryDTO;
import com.lhb.entity.User;
import com.lhb.properties.JwtProperties;
import com.lhb.result.PageResult;
import com.lhb.result.Result;
import com.lhb.service.UserService;
import com.lhb.utils.JwtUtil;
import com.lhb.vo.UserGetByIdVO;
import com.lhb.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/admin/user")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/pageQuery")
    @ApiOperation("用户分页查询")
    public Result<PageResult> pageQuery(@RequestBody UserPageQueryDTO userPageQueryDTO)
    {
        log.info("用户分页查询:{}", userPageQueryDTO);

        PageResult pageResult=userService.pageQuery(userPageQueryDTO);


        return Result.success(pageResult);
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户")
    public Result<UserGetByIdVO> getById(@PathVariable Integer id)
    {
        log.info("根据id:{}查询用户",id);

        UserGetByIdVO userGetByIdVO =userService.getById(id);

        return Result.success(userGetByIdVO);
    }


    @PutMapping
    @ApiOperation("编辑用户")
    public Result update(@RequestBody UserEditDTO userEditDTO)
    {
        log.info("更新用户id为:{}的数据",userEditDTO);

       userService.update(userEditDTO);

        return Result.success();
    }

    @PutMapping("/status/{status}")
    @ApiOperation("修改用户状态")
    public Result modifyStatus(@PathVariable Integer status,Long id)
    {
            log.info("修改用户id为:{}的状态",id);

            userService.modifyStatus(status,id);


        return Result.success();
    }



    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除用户")
    public Result deleteById(@PathVariable Integer id)
    {
        log.info("删除id为:{}的用户",id);

        userService.deleteById(id);

        return Result.success();
    }

    @PostMapping
    @ApiOperation("添加用户")
    public Result save(@RequestBody UserAddDTO userAddDTO)
    {
        log.info("添加用户:{}",userAddDTO);

        userService.save(userAddDTO);


        return Result.success();
    }

    /**
     * 登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO)
    {
            log.info("用户登录:{}", userLoginDTO);

           User user= userService.login(userLoginDTO);

        //生成令牌
        HashMap<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

        //生成返回数据
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .userName(user.getUsername())
                .realName(user.getRealName())
                .avatarImage(user.getAvatarImage())
                .token(token)
                .build();


        return Result.success(userLoginVO);
    }

    /**
     * 用户退出
     * @return
     */
    @PostMapping("/logOut")
    @ApiOperation("用户退出")
    public Result logOut()
    {

        return Result.success();
    }
}
